
/**************************************************************************
Step 1. Include necessary header files such that the stuff your test logic needs is declared. 
Don't forget gtest.h, which declares the testing framework.                                                   
**************************************************************************/
#include "gtest/gtest.h"
#include "include/pf_arg.h"


/**************************************************************************
Step 2. Use test fixture to design your complex cases. 
You can add SetUp() to initialize the test data and 
TearDown() to clear the test data for common usage
Pls remember that when you using test fixture, need to use macro "TEST_F" instead of "TEST"                                                 
**************************************************************************/
class PfArgTest : public testing::Test 
{
    /* This SetUp() will be called before every TEST CASE which is marked by macro "TEST_F" */
    virtual void SetUp() 
    {

    }

    /* This TearDown will be called after every TEST CASE which is marked by macro "TEST_F" */
    virtual void TearDown() 
    {

    }

};

/***************************************************************************
Step 3. Use macro "TEST_F" to start your test case                                      
You can design your test case as the example
***************************************************************************/
TEST_F(PfArgTest, pf_arg_parse)
{
    /* Define the local variables */
    int argc1 = 1;
    char* argv1[1];
    argv1[0] = "kkkkk1";
    
    int argc2 = 2;
    char* argv2[2];
    argv2[0] = "kkkkk2";
    argv2[1] = "-abc=1234";

    int argc3 = 7;
    char* argv3[7];
    argv3[0] = "kkkkk3";
    argv3[1] = "-abc=1234";
    argv3[2] = "--abc=5678";
    argv3[3] = "-------------abc=90";
    argv3[4] = "--cde=value\"11";
    argv3[5] = "--1234=";
    argv3[6] = "--=kjkjkj";


    /* initilize test data for this case */
    

    /* Call your function for test */
    /* only one execute*/
	pf_arg_t* arg_t1 = pf_arg_parse(argc1, argv1);
    /* one execute and one arg param */
	pf_arg_t* arg_t2 = pf_arg_parse(argc2, argv2);
    /* one execute and more arg param */
	pf_arg_t* arg_t3 = pf_arg_parse(argc3, argv3);

    /* Check the result */
	EXPECT_NE((void*)NULL, arg_t1);
	EXPECT_NE((void*)NULL, arg_t2);
	EXPECT_NE((void*)NULL, arg_t3);

    /* Clear the test data for this case */
    pf_arg_clear(arg_t1);
    pf_arg_clear(arg_t2);
    pf_arg_clear(arg_t3);
}

TEST_F(PfArgTest,pf_arg_exec)
{
    int argc1 = 1;
    char* argv1[1];
    argv1[0] = "kkkkk1";
    
    int argc2 = 2;
    char* argv2[2];
    argv2[0] = "kkkkk2";
    argv2[1] = "-abc=1234";

    int argc3 = 7;
    char* argv3[7];
    argv3[0] = "kkkkk3";
    argv3[1] = "-abc=1234";
    argv3[2] = "--abc=5678";
    argv3[3] = "-------------abc=90";
    argv3[4] = "--cde=value\"11";
    argv3[5] = "--1234=";
    argv3[6] = "--=kjkjkj";

    pf_arg_t* arg_t1 = pf_arg_parse(argc1, argv1);
	pf_arg_t* arg_t2 = pf_arg_parse(argc2, argv2);
	pf_arg_t* arg_t3 = pf_arg_parse(argc3, argv3);

    /* only one execute*/
    const char* exec1 = pf_arg_exec(arg_t1);
    /* one execute and one arg param */
    const char* exec2 = pf_arg_exec(arg_t2);
    /* one execute and more arg param */
    const char* exec3 = pf_arg_exec(arg_t3);
    /* NULL param */
    const char* exec4 = pf_arg_exec(NULL);

    EXPECT_STREQ(exec1,"kkkkk1");
    EXPECT_STREQ(exec2,"kkkkk2");
    EXPECT_STREQ(exec3,"kkkkk3");
    EXPECT_EQ(exec4,(void*)NULL);
   
   
    pf_arg_clear(arg_t1);
    pf_arg_clear(arg_t2);
    pf_arg_clear(arg_t3);
}

TEST_F(PfArgTest,pf_arg_find)
{
    int argc1 = 1;
    char* argv1[1];
    argv1[0] = "kkkkk1";
    
    int argc2 = 2;
    char* argv2[2];
    argv2[0] = "kkkkk2";
    argv2[1] = "-abc=1234";

    int argc3 = 8;
    char* argv3[8];
    argv3[0] = "kkkkk3";
    argv3[1] = "-abc=1234";
    argv3[2] = "--abc=5678";
    argv3[3] = "-------------abc=90";
    argv3[4] = "--cde=value\"11";
    argv3[5] = "--1234=";
    argv3[6] = "--=kjkjkj";
    argv3[7] = "--ddd-ee--ff=kjkjkj";

    pf_arg_t* arg_t1 = pf_arg_parse(argc1, argv1);
	pf_arg_t* arg_t2 = pf_arg_parse(argc2, argv2);
	pf_arg_t* arg_t3 = pf_arg_parse(argc3, argv3);

    /* only one execute*/
    const char* value1 = pf_arg_find(arg_t1,"kkkkk1");
    EXPECT_EQ(value1,(void*)NULL);

    /* one execute and one arg param */
    const char* value2 = pf_arg_find(arg_t2,"abc");
    EXPECT_STREQ(value2,"1234");  
    value2 = pf_arg_find(arg_t2,"-abc");
    EXPECT_EQ(value2,(void*)NULL);  
    value2 = pf_arg_find(arg_t2,"-abc=");
    EXPECT_EQ(value2,(void*)NULL);  

    /* one execute and more arg param */
    const char* value3 = pf_arg_find(arg_t3,"abc");
    EXPECT_STREQ(value3,"90");
    value3 = pf_arg_find(arg_t3,"-abc");
    EXPECT_EQ(value3,(void*)NULL);
    value3 = pf_arg_find(arg_t3,"--abc");
    EXPECT_EQ(value3,(void*)NULL);
    value3 = pf_arg_find(arg_t3,"cde");
    EXPECT_STREQ(value3,"value\"11");
    value3 = pf_arg_find(arg_t3,"1234");
    EXPECT_STREQ(value3,"");
    value3 = pf_arg_find(arg_t3,"");
    EXPECT_STREQ(value3,"");
    value3 = pf_arg_find(arg_t3,"ff");
    EXPECT_STREQ(value3,(char*)NULL);
    value3 = pf_arg_find(arg_t3,"ee--ff");
    EXPECT_STREQ(value3,(char*)NULL);
    value3 = pf_arg_find(arg_t3,"ddd-ee--ff");
    EXPECT_STREQ(value3,"kjkjkj");

    /* NULL param */
    const char* value4 = pf_arg_find(NULL,"abc");
    EXPECT_EQ(value4,(void*)NULL);
    const char* value5 = pf_arg_find(arg_t3,NULL);
    EXPECT_EQ(value5,(void*)NULL);

    pf_arg_clear(arg_t1);
    pf_arg_clear(arg_t2);
    pf_arg_clear(arg_t3);

}

TEST_F(PfArgTest,pf_arg_clear)
{
    int argc1 = 1;
    char* argv1[1];
    argv1[0] = "kkkkk1";
    
    int argc2 = 2;
    char* argv2[2];
    argv2[0] = "kkkkk2";
    argv2[1] = "-abc=1234";

    int argc3 = 7;
    char* argv3[7];
    argv3[0] = "kkkkk3";
    argv3[1] = "-abc=1234";
    argv3[2] = "--abc=5678";
    argv3[3] = "-------------abc=90";
    argv3[4] = "--cde=value\"11";
    argv3[5] = "--1234=";
    argv3[6] = "--=kjkjkj";

    pf_arg_t* arg_t1 = pf_arg_parse(argc1, argv1);
	pf_arg_t* arg_t2 = pf_arg_parse(argc2, argv2);
	pf_arg_t* arg_t3 = pf_arg_parse(argc3, argv3);

   
    /* only one execute*/
    int ret1 = pf_arg_clear(arg_t1);
    /* one execute and one arg param */
    int ret2 = pf_arg_clear(arg_t2);
    /* one execute and more arg param */
    int ret3 = pf_arg_clear(arg_t3);
    /* NULL param */
    int ret4 = pf_arg_clear(NULL);

    EXPECT_EQ(ret1,0); 
    EXPECT_EQ(ret2,0); 
    EXPECT_EQ(ret3,0); 
    EXPECT_NE(ret4,0); 
}
